<template>
  <div class="container">
    <div class="app-container">
      <!-- 角色管理内容 -->
      <div class="role-operate">
        <el-button size="mini" type="primary" @click="addRole">添加角色</el-button>
      </div>
      <!-- 放置table组件 -->
      <el-table :data="list">
        <!-- 放置列 -->
        <el-table-column prop="name" align="center" width="200" label="角色">
          <template slot-scope="{ row }">
            <el-input v-if="row.isEdit" v-model="row.editRow.name" size="mini" />
            <span v-else>{{ row.name }}</span>
          </template>
        </el-table-column>

        <el-table-column prop="state" align="center" width="200" label="启用">
          <template slot-scope="{row}">
            <el-switch
              v-if="row.isEdit"
              v-model="row.editRow.state"
              :active-value="1"
              :inactive-value="0"
              size="mini"
            />
            <span v-else>{{ row.state===1?"已启用":"未启用" }}</span>
          </template>
        </el-table-column>

        <el-table-column prop="description" align="center" label="描述">
          <template slot-scope="{row}">
            <el-input v-if="row.isEdit" v-model="row.editRow.description" size="mini" />
            <span v-else>{{ row.description }}</span>
          </template>
        </el-table-column>

        <el-table-column align="center" label="操作">
          <!-- 放置操作按钮 -->

          <template slot-scope="{row}">
            <!-- 编辑状态 -->
            <template v-if="row.isEdit">
              <el-button type="primary" size="mini" @click="hOK(row)">确定</el-button>
              <el-button size="mini" @click="row.isEdit=false">取消</el-button>
            </template>

            <template v-else>
              <!-- 非编辑状态 -->
              <el-button size="mini" type="text">分配权限</el-button>
              <el-button size="mini" type="text" @click="isEdit(row)"> 编辑</el-button>
              <!-- 气泡框 -->
              <el-popconfirm
                confirm-button-text="好的"
                cancel-button-text="不用了"
                icon="el-icon-info"
                icon-color="red"
                title="这是一段内容确定删除吗？"
                @onConfirm="del(row.id)"
              >
                <el-button slot="reference" size="small" type="text">删除</el-button>
              </el-popconfirm>
            </template>
          </template>

        </el-table-column>
      </el-table>
      <!-- 放置分页组件 -->
      <el-row type="flex" style="height:60px" align="middle" justify="end">
        <!-- 放置分页组件
         total:总数据条数
        background 背景色
        page-size:当前页码数
        current-page:当前页面的数据条数
        @current-change="hCurrentChange":当前点击的页码触发事件 -->

        <el-pagination
          :current-page="page"
          :page-sizes="[5, 10, 15]"
          :page-size="pagesize"
          layout="sizes, prev, pager, next"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />

        <!-- <el-pagination layout="prev, pager, next" /> -->
      </el-row>
    </div>

    <el-dialog width="500px" title="新增角色" :visible.sync="showDialog" @close="hClose">
      <!-- 表单内容 -->
      <el-form ref="addform" label-width="120px" :model="addform" :rules="rules">
        <el-form-item label="角色名称" prop="name">
          <el-input v-model="addform.name" style="width:300px" size="mini" />
        </el-form-item>
        <el-form-item label="启用">
          <!-- 如果不需要校验 就不需要写 prop属性 -->
          <!-- 开关 -->
          <el-switch
            v-model="addform.state"
            :active-value="1"
            :inactive-value="0"
            size="mini"
          />
        </el-form-item>
        <el-form-item label="角色描述" prop="description">
          <el-input v-model="addform.description" type="textarea" :rows="3" style="width:300px" size="mini" />
        </el-form-item>
        <el-form-item>
          <el-row type="flex" justify="center">
            <el-col :span="12">
              <el-button type="primary" size="mini" @click="hSave">确定</el-button>
              <el-button size="mini" @click="hClose">取消</el-button>
            </el-col>
          </el-row>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { addRole, delRole, editRole, getRoleList } from '@/api/role'
export default {
  name: 'Social',
  data() {
    return {
      total: 0,
      page: 1,
      pagesize: 5,
      list: [],
      showDialog: false,
      addform: {
        name: '',
        description: '',
        state: 1 // 默认未1启用 关闭 0 打开1
      },
      rules: {
        name: [{ required: true, message: '角色名称不能为空', trigger: 'blur' }],
        description: [{ required: true, message: '角色描述不能为空', trigger: 'blur' }]
      }
    }
  },
  created() {
    this.getRoleList()
  },
  methods: {
    // 获取角色列表
    async getRoleList() {
      const res = await getRoleList({ page: this.page, pagesize: this.pagesize })
      this.list = res.data.rows
      this.total = res.data.total
      // this.$set(目标对象, 属性名称, 初始值) 可以针对目标对象 添加的属性 添加响应式
      // 初始化时缓存数据
      this.list.forEach(it => {
        this.$set(it, 'isEdit', false)
        this.$set(it, 'editRow', {
          id: it.id,
          name: it.name,
          state: it.state,
          description: it.description
        })
      })
      // console.log(this.list)
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`)
      // 1.更新条数
      this.pagesize = val
    },
    handleCurrentChange(page) {
      // console.log(`当前页: ${page}`)
      // 1.更新页码
      this.page = page
      // 2.重发请求
      this.getRoleList()
    },
    // 添加角色 打开弹框
    addRole() {
      this.showDialog = true
    },
    hSave() {
      this.$refs.addform.validate(valid => {
        valid && this.doAdd()
      })
    },
    // 添加功能
    async doAdd() {
      await addRole(this.addform)
      this.getRoleList()
      this.$message.success('新增成功')
      // 更新表格 添加后显示添加的位置
      this.total = this.total + 1
      // Math.ceil向上取整
      this.page = Math.ceil(this.total / this.pagesize)
      this.hClose()
    },
    // 重置表单 关闭弹层
    hClose() {
      this.$refs.addform.resetFields()
      this.showDialog = false
    },
    // 编辑功能 行内编辑
    isEdit(row) {
      row.isEdit = true // 改变行的编辑状态
      // console.log(row)
    },
    async hOK(row) {
      // 判断是否必填
      console.log(row)
      if (!row.editRow.name || !row.editRow.description) {
        this.$message.error('请填写正确内容')
      }
      // 调用修改接口
      await editRole(row.editRow)
      this.$message.success('修改成功')
      // 退出编辑
      // 重发请求
      this.getRoleList()
    },
    async del(id) {
      await delRole(id)
      this.$message.success('删除成功')
      // 删除的如果是最后一个
      if (this.list.length === 1) this.page--
      this.getRoleList()
    }
  }
}
</script>

<style scoped>
.role-operate {
  padding: 10px;
}
</style>
